package com.bawei.mall.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.bawei.common.core.utils.JwtUtils;
import com.bawei.common.core.utils.StringUtils;
import com.bawei.common.security.utils.SecurityUtils;
import com.bawei.mall.user.domain.CusUser;
import com.bawei.mall.user.domain.MallProductFootPrint;
import com.bawei.mall.user.mapper.MallProductFootPrintMapper;
import com.bawei.mall.user.service.MallProductFootPrintService;
import com.bawei.system.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：HaoYunde
 * @Package：com.bawei.mall.user.service.impl
 * @Project：mall_cloud
 * @name：MallProductFootPrintServiceImpl
 * @Date：2024/7/2 16:49
 */
@Service
public class MallProductFootPrintServiceImpl implements MallProductFootPrintService {
    @Autowired
    private MallProductFootPrintMapper mallProductFootPrintMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    private static final String ACCESS_TOKEN="login_tokens:";

    @Override
    public List<MallProductFootPrint> readHistory() {
        String token = SecurityUtils.getToken();
        if(StringUtils.isEmpty(token)){
            throw new RuntimeException("token空了");
        }
        String key = JwtUtils.getUserKey(token);
        String userKey = getTokenKey(key);
        String o = redisTemplate.opsForValue().get(userKey);
        LoginUser loginUser = JSON.parseObject(o, LoginUser.class);
        CusUser cusUser = loginUser.getCusUser();
        Integer userId = cusUser.getUserId();
        //根据登录人id查询足迹表
        List<MallProductFootPrint> list=mallProductFootPrintMapper.readHistory(userId);
        return list;
    }

    @Override
    public void deleteAllFootprints() {
        String token = SecurityUtils.getToken();
        if(StringUtils.isEmpty(token)){
            throw new RuntimeException("token空了");
        }
        String key = JwtUtils.getUserKey(token);
        String userKey = getTokenKey(key);
        String o = redisTemplate.opsForValue().get(userKey);
        LoginUser loginUser = JSON.parseObject(o, LoginUser.class);
        CusUser cusUser = loginUser.getCusUser();
        Integer userId = cusUser.getUserId();
        mallProductFootPrintMapper.deleteAllFootprints(userId);
    }

    @Override
    public void create(MallProductFootPrint mallProductFootPrint) {
        String token = SecurityUtils.getToken();
        if(StringUtils.isEmpty(token)){
            throw new RuntimeException("token空了");
        }
        String key = JwtUtils.getUserKey(token);
        String userKey = getTokenKey(key);
        String o = redisTemplate.opsForValue().get(userKey);
        LoginUser loginUser = JSON.parseObject(o, LoginUser.class);
        CusUser cusUser = loginUser.getCusUser();
        mallProductFootPrint.setUserId(cusUser.getUserId());
        mallProductFootPrint.setUserName(cusUser.getUsername());
        mallProductFootPrint.setUserIcon(cusUser.getAvatarUrl());
        mallProductFootPrintMapper.creat(mallProductFootPrint);
    }


    private String getTokenKey(String key)
    {
        return ACCESS_TOKEN + key;
    }

}
